package net.code.btalk.ui;

import javax.microedition.io.StreamConnection;
import javax.microedition.lcdui.Display;

import com.hoodemia.core.RegisterAccount;
import com.hoodemia.ui.StandardButton;
import com.hoodemia.ui.StandardPasswordField;
import com.hoodemia.ui.StandardTextField;

import net.code.btalk.BTalk;
import net.code.btalk.BTalkResource;
import net.code.btalk.ServerDef;
import net.code.md5.MD5;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.BitmapField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.FullScreen;
import net.rim.device.api.ui.container.HorizontalFieldManager;
import net.rim.device.api.ui.container.VerticalFieldManager;
import net.rim.device.api.ui.decor.Background;
import net.rim.device.api.ui.decor.BackgroundFactory;
import net.sourceforge.jxa.XmlReader;
import net.sourceforge.jxa.XmlWriter;

public class LoginScreen extends FullScreen {

	public static BTalk btalk;
	RegisterAccount ra;

	public LoginScreen(boolean saved, String username, String password,
			int serverType, String server, boolean usessl) {
		super(FullScreen.VERTICAL_SCROLL | FullScreen.VERTICAL_SCROLLBAR);
		// background uses black wood texture
		Background background = BackgroundFactory.createBitmapBackground(Bitmap
				.getBitmapResource(Constants.IMAGE_BACKGROUND_LOGIN));
		this.setBackground(background);

		// add row space
		this.add(new LabelField());

		// hoodemia logo and letter
		HorizontalFieldManager logoSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);

		Bitmap hoodemiaLogo = Bitmap.getBitmapResource(Constants.IMAGE_HOODEMIA_LOGO);
		BitmapField hoodemiaLogoField = new BitmapField(hoodemiaLogo,
				BitmapField.FIELD_HCENTER);

		Bitmap hoodemiaLetter = Bitmap.getBitmapResource(Constants.IMAGE_HOODEMIA_LETTER);
		BitmapField hoodemiaLetterField = new BitmapField(hoodemiaLetter,
				BitmapField.FIELD_HCENTER);

		logoSection.add(hoodemiaLogoField);
		logoSection.add(hoodemiaLetterField);

		this.add(logoSection);

		// add row spaces
		this.add(new LabelField());
		this.add(new LabelField());
		this.add(new LabelField());

		// sign in form
		VerticalFieldManager signInForm = new VerticalFieldManager(
				Field.FIELD_VCENTER);

		// configure username field
		HorizontalFieldManager usernameSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		usernameSection.setPadding(Constants.FORM_SECTION_H_PADDING);

		Bitmap usernameIcon = Bitmap.getBitmapResource(Constants.IMAGE_USERNAME_ICON);
		BitmapField usernameIconField = new BitmapField(usernameIcon);
		usernameIconField.setPadding(Constants.ICON_FIELD_PADDING);

		final StandardTextField usernameField = new StandardTextField("username", 100, StandardTextField.FIELD_HCENTER & StandardTextField.NO_NEWLINE);

		usernameSection.add(usernameIconField);
		usernameSection.add(usernameField);

		signInForm.add(usernameSection);
		signInForm.add(new LabelField());

		// configure password field
		HorizontalFieldManager passwordSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);
		passwordSection.setPadding(Constants.FORM_SECTION_H_PADDING);

		Bitmap passwordIcon = Bitmap.getBitmapResource(Constants.IMAGE_PASSWORD_ICON);
		BitmapField passwordIconField = new BitmapField(passwordIcon);
		passwordIconField.setPadding(Constants.ICON_FIELD_PADDING);

		final StandardPasswordField passwordField = new StandardPasswordField("password", 100, StandardPasswordField.FIELD_HCENTER & StandardTextField.NO_NEWLINE);
		
		passwordSection.add(passwordIconField);
		passwordSection.add(passwordField);

		signInForm.add(passwordSection);
		signInForm.add(new LabelField());

		// configure sign in button
		// configure listener
		FieldChangeListener loginButtonListener = new FieldChangeListener() {
			public void fieldChanged(Field field, int context) {
				String username = usernameField.getText();
				String password = passwordField.getText();
				
				/*String username = "fahri";
				String password = "nazda";*/

				if (username.length() == 0 || password.length() == 0) {
					Dialog.alert(BTalkLocale
							.getString(BTalkResource.LOGIN_SCREEN_ALERT_UP_INVALID));
				} else {
					username += "@hoodemia.com";
					password += "jcow";

					// convert plaintext into bytes
					byte passwordBytes[] = password.getBytes();
					// create MD5 object
					MD5 md5 = new MD5(passwordBytes);
					// get the resulting hashed byte
					byte[] hashedBytes = md5.doFinal();
					// convert the hashed byte into hexadecimal character for
					// display
					String hashResult = MD5.toHex(hashedBytes);

//					LoginScreen.this.username = username;
//					LoginScreen.this.password = hashResult;

					ServerDef serverDef = new ServerDef();
					serverDef.serverType = 1;
					serverDef.server = "hoodemia.com";
					// by default-use ssl (it's a must!)
					serverDef.usessl = true;
					if (serverDef.usessl)
						serverDef.port = "5223";
					else
						serverDef.port = "5222";
					// use wifi not supported - yet
					serverDef.useWifi = false;
					
//					Dialog.alert(hashResult);

					btalk.loginJxa(username, hashResult, serverDef);
				}
			}
		};

		StandardButton loginButton = new StandardButton("Sign In", StandardButton.FIELD_HCENTER, StandardButton.PADDING_SMALL);
		loginButton.setChangeListener(loginButtonListener);
		signInForm.add(loginButton);

		this.add(signInForm);

		this.add(new LabelField());
		this.add(new LabelField());

		HorizontalFieldManager moreLinkSection = new HorizontalFieldManager(
				Field.FIELD_HCENTER);

		FieldChangeListener forgotLinkListener = new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				Dialog.alert("Sorry sir, nothing I could provide now");
			}

		};
		
		// configure forgot password link
		HyperlinkButtonField forgotLink = new HyperlinkButtonField(
				"Forgot Password", Color.WHITE, 0, Color.DARKORANGE, 0, 0);
		// forgotLink.setFont(FONT_BOLD_STYLE);
		forgotLink.setChangeListener(forgotLinkListener);
		moreLinkSection.add(forgotLink);

		// separator
		moreLinkSection.add(new LabelField(" | ", Field.FIELD_HCENTER) {
			public void paint(Graphics g) {
				g.setColor(Color.WHITE);
				super.paint(g);
			}
		});

		// configure sign up link
		// configure listener
		FieldChangeListener signupLinkListener = new FieldChangeListener() {

			public void fieldChanged(Field field, int context) {
				// TODO Auto-generated method stub
				SignUpScreen signupScr = new SignUpScreen();
				UiApplication.getUiApplication().pushScreen(signupScr);
			}

		};

		HyperlinkButtonField signupLink = new HyperlinkButtonField("Sign Up",
				Color.WHITE, 0, Color.DARKORANGE, 0, 0);
		// signupLink.setFont(FONT_BOLD_STYLE);
		moreLinkSection.add(signupLink);
		signupLink.setChangeListener(signupLinkListener);

		this.add(moreLinkSection);
		this.add(new LabelField());

		/*
		 * debugField = new LabelField(">>debug info", LabelField.USE_ALL_WIDTH)
		 * {
		 * 
		 * protected void paint(Graphics graphics) { // TODO Auto-generated
		 * method stub graphics.setColor(Color.WHITE);
		 * graphics.setBackgroundColor(Color.DARKGRAY); graphics.clear();
		 * super.paint(graphics); }
		 * 
		 * };
		 */
	}
	
	public boolean onClose() {
		this.close();
		return true;
	}

	/*private SVGImage loadSVGImage(String url) throws IOException {
		// Open our input stream of the svg file we want to load.
		InputStream inputStream = getClass().getResourceAsStream(url);
		// Load our svg image from the input stream.
		return (SVGImage) SVGImage.createImage(inputStream, null);
	}*/
}
